package com.xiangban.xiangbanmall.service;

import com.xiangban.xiangbanmall.dao.MemberMapper;
import com.xiangban.xiangbanmall.dao.TicketMapper;
import com.xiangban.xiangbanmall.dao.UserMapper;
import com.xiangban.xiangbanmall.exception.BizException;
import com.xiangban.xiangbanmall.fegin.UserServiceFeign;
import com.xiangban.xiangbanmall.model.TUser;
import com.xiangban.xiangbanmall.model.TUserMember;
import com.xiangban.xiangbanmall.model.TUserMemberGrade;
import com.xiangban.xiangbanmall.model.TUserTicket;
import com.xiangban.xiangbanmall.model.dto.User;
import com.xiangban.xiangbanmall.model.res.ApiResultCode;
import com.xiangban.xiangbanmall.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.xiangban.xiangbanmall.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/10/27 8:57
 */
@Service
public class UserService {

    @Autowired
    private UserMapper tUserMapper;
    @Autowired
    private UserServiceFeign userServiceFeign;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private UserMemberService userMemberService;
    @Autowired
    private MemberMapper memberMapper;


    public void addTUser(TUser tUser) {
        tUserMapper.insertTUser(tUser);

    }

    @Transactional
    public void updateTUser(TUser tUser) {
        tUser.setUid(Integer.parseInt(commonUtils.getUserIdByToken()));

        tUserMapper.updateTUserById(tUser);
        HashMap<String, Object> objectHashMap = new HashMap<>();
        if (tUser.getNickName() != null){
            objectHashMap.put("nickName",tUser.getNickName());
        }
        if (tUser.getImage() != null){
            objectHashMap.put("image",tUser.getImage());
        }
        if (tUser.getSex() != null){
            objectHashMap.put("sex",tUser.getSex());
        }
        objectHashMap.put("uid",tUser.getUid());
        userServiceFeign.updateUserInfo(objectHashMap);
    }

    /** 获取用户信息
     * @param
     */
    public Object getTUser() {
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
        User user = new User();
        if (BizEmpty(commonUtils.getUserIdByToken())){
            return user;
        }
        int userId = Integer.parseInt(commonUtils.getUserIdByToken());
        user= tUserMapper.selectTUserById(userId);
        if (user != null){
            if (user.getMoney() == null){
                user.setFlag(1);//1:未开通过会员,2:已开通会员
                user.setMoney(new BigDecimal(0));
                user.setIntegral(new BigDecimal(0));
            }else {
                List<TUserTicket> userTicketByUid = ticketMapper.getUserTicketByUid(userId);
                user.setTUserTicket(userTicketByUid);
                user.setFlag(2);
            }
        }
        return user;
    }


    public void updateOpenIdById(TUser tUser) {
        if (BizEmpty(tUser.getUid())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(tUser.getOpenId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        tUserMapper.updateOpenIdById(tUser);

        TUserMember userMember = memberMapper.getUserMember(tUser.getUid());
        if (userMember == null){
            userMemberService.registerMember(0,tUser.getUid());
        }
    }
}
